// Declaration de variables externes.
uniform extern float4x4 gWVP;
uniform extern float4x4 gViewProj;
uniform extern texture gTex;
uniform extern float3 gEyePosW;

sampler TexS = sampler_state
{
	Texture = <gTex>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU = WRAP;
	AddressV = WRAP;
};

// Concordance de la structure de vertex CPU-GPU
struct OutputVS
{
	float4 posH : POSITION0;
	float4 color : COLOR0;
	float2 tex0 : TEXCOORD0;
};

OutputVS GrassVS(float3 posL : POSITION0, float3 quadPosW : TEXCOORD1, float2 tex0 : TEXCOORD1,float amplitude : TEXCOORD2, float4 color : COLOR0)
{
	OutputVS outVS = (OutputVS)0;
	
	float3 look = normalize(gEyePosW - quadPosW);
	float3 right = normalize(cross(float3(0.0f,1.0f,0.0f), look));
	float3 up = cross(look,right);

	float4x4 lookAtMtx;
	lookAtMtx[0] = float4(right, 0.0f);
	lookAtMtx[1] = float4(up, 0.0f);
	lookAtMtx[2] = float4(look, 0.0f);
	lookAtMtx[3] = float4(quadPosW, 1.0f);

	float4 posW = mul(float4(posL, 1.0f), lookAtMtx);

	outVS.posH = mul(posW,gWVP);
	outVS.color = color;
	outVS.tex0 = tex0;
	
	return outVS;
	
}

// Fonction Vertex Shader
OutputVS TransformVS(float3 posL : POSITION0, float2 tex0 : TEXCOORD0, float4 color: COLOR0)
{
	OutputVS outVS = (OutputVS)0;

	outVS.posH = mul(float4(posL, 1.0f), gWVP);

	outVS.color = color;
	outVS.tex0 = tex0;

	return outVS;
}


float4 TransformPS( float4 color: COLOR0
				  , float2 tex0 : TEXCOORD0) : COLOR
{
	float3 texColor = tex2D(TexS, tex0).rgb;

	//return texColor;
	return float4(texColor, 1);
}

technique TransformTech
{
	pass P0
	{
		vertexShader = compile vs_2_0 TransformVS();
		pixelShader = compile ps_2_0 TransformPS();

		FillMode = Solid;
	}
}

technique TransformGrass
{
	pass P0
	{
		vertexShader = compile vs_2_0 GrassVS();
		pixelShader = compile ps_2_0 TransformPS();

		AlphaRef = 200;
		AlphaFunc = GreaterEqual;
		AlphaTestEnable = true;

		CullMode = None;
		//FillMode = Solid;
	}
}